package it.cast.zhang05.ui;

import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.OvershootInterpolator;
import android.widget.ImageView;
import android.widget.ListView;

/**
 * Created by Administrator on 2016/4/11.
 */
public class parallaxDemo extends ListView {
    private ImageView iv_header;
    private int orignalHeight;
    private int drawHeight;

    public parallaxDemo(Context context) {
        this(context, null);
    }

    public parallaxDemo(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public parallaxDemo(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    //把ImageView 的索引传过来
    public void setParallaxImage(ImageView iv_header) {
        this.iv_header = iv_header;
        //图片的初始高度
        orignalHeight = iv_header.getHeight();
        //图片的原始高度
        drawHeight = iv_header.getDrawable().getIntrinsicHeight();
       /* System.out.println("===="+width);
        System.out.println("---------"+didth1);*/
    }


    @Override
    protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
        /*
        * **deltay:竖直方向滑动的瞬时变化量，上为+，下为-
			ScrollY:两端滑动超出的距离
			ScrollRangeY:滑动的范围
			maxOverScrollY:竖直方向最大的滑动位置
		***	isTouchEvent:是否是用户触摸拉动，true表示用户手指触摸拉动
        * */
        if(deltaY<0&&isTouchEvent) {//下拉操作，并且是触摸拉动
            //把deltay的绝对值累计加给header
            int newHeight = iv_header.getHeight() + Math.abs(deltaY/3);
            if(newHeight<=drawHeight) {
                //让新的增值有效
                iv_header.getLayoutParams().height = newHeight;
                iv_header.requestLayout();
            }
        }

        return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_UP:
                //把当前的头布局恢复到原来的初始高度
                int currentHeight = iv_header.getHeight();
                //300-160
                //平滑滚动
                ValueAnimator va = ValueAnimator.ofInt(currentHeight,orignalHeight);
                va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        //获取动画执行过程中的分度值
                       float fraction =  animation.getAnimatedFraction();

                        //获取中间的值
                       Integer animateValue = (Integer) animation.getAnimatedValue();
                        System.out.println(fraction+"----------"+animateValue);

                        //让新的增值有效
                        iv_header.getLayoutParams().height = animateValue;
                        iv_header.requestLayout();
                    }
                });
                va.setInterpolator(new OvershootInterpolator(4));
                va.setDuration(500);
                va.start();
                break;
        }
        return super.onTouchEvent(ev);
    }
}
